﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;
using XLCS.Common;
using System.Diagnostics;
using System.Data.SqlServerCe;
using CustomControl.NewXLAGControl;
using SqlCeViewer.CommonMethod;

namespace SqlCeViewer.Moduls
{
    public partial class TestPerformance : BaseSubForm
    {
        public TestPerformance()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            int recordCount = 10000;
            using (PopupForm p = new PopupForm())
            {
                p.X_NotifyStr = "Record Count";
                if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    recordCount = int.Parse(p.X_Result);
                }
                else
                {
                    return;
                }
            }

            string testDb=DateTime.Now.Second.ToString()+"test.sdf";
            string testTable="testTable";

            if (!GlobalInfo.MainEngineer.CreateDataBase(testDb))
            {
                return;
            }
            if (!GlobalInfo.MainEngineer.OpenDatabase(testDb))
            {
                return;
            }
            List<CreateTableArgs> argsList=new List<CreateTableArgs>();
            CreateTableArgs args=new CreateTableArgs();
            args.allowNulls = false;
            args.dataLength = 0;
            args.dataType="int";
            args.fieldName="id";
            args.isUnique = false;
            args.isPrimaryKey = false;
            argsList.Add(args);

            try
            {
                if (!GlobalInfo.MainEngineer.CreateTableWithSchemaInfo(argsList, testTable))
                {
                    return;
                }
                string sqlCmd = string.Empty;
                SqlCeConnection conn = new SqlCeConnection(string.Format("Data source={0}", testDb));
                SqlCeCommand cmd = new SqlCeCommand();
                cmd.Connection = conn;
                conn.Open();

                Stopwatch watch = new Stopwatch();
                watch.Start();
                for (int i = 0; i < recordCount; i++)
                {
                    sqlCmd = string.Format("insert into {0} values ({1})", testTable, i);
                    cmd.CommandText = sqlCmd;
                    cmd.ExecuteNonQuery();
                }

                watch.Stop();
                long sqlDirectTime = watch.ElapsedMilliseconds;

                watch.Reset();
                watch.Start();

                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (?)", testTable);
                cmd.Parameters.Add("@id", SqlDbType.Int);
                cmd.Prepare();
                for (int i = 0; i < recordCount; i++)
                {
                    cmd.Parameters[0].Value = i;
                    cmd.ExecuteNonQuery();
                }
                watch.Stop();
                long sqlParaTime = watch.ElapsedMilliseconds;
                watch.Reset();

                watch.Start();

                cmd.CommandText = testTable;
                cmd.CommandType = CommandType.TableDirect;
                SqlCeResultSet st = cmd.ExecuteResultSet(ResultSetOptions.Updatable);
                SqlCeUpdatableRecord rec = st.CreateRecord();
                for (int i = 0; i < recordCount; i++)
                {
                    rec.SetInt32(0, i);
                    
                    st.Insert(rec);
                }
                watch.Stop();
                long sqlceResultSetTime = watch.ElapsedMilliseconds;

                //watch.Start();

                //cmd.CommandText = testTable;
                //cmd.CommandType = CommandType.TableDirect;
                //SqlCeResultSet st = cmd.ExecuteResultSet(ResultSetOptions.Updatable);
                //SqlCeUpdatableRecord rec = 
                //for (int i = 0; i < recordCount; i++)
                //{
                //    rec.SetInt32(0, i);

                //    st.Insert(rec);
                //}
                //watch.Stop();
                long sqlceUpdateResultSetTime = 100;// watch.ElapsedMilliseconds;



                cmd.Dispose();
                conn.Close();

                MessageBox.Show(string.Format("Test Result is \r\nDirect sql command used {0} \r\nUse parameters used{1}\r\nUse SqlceResultSet used{2}\r\nUpdate Sqlce ResultSet{3}\r\n", sqlDirectTime, sqlParaTime, sqlceResultSetTime,sqlceUpdateResultSetTime));
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }
        }
    }
}